/**
 * 
 */
package cre.gutwo.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cre.gutwo.dao.jpa.CompanyCodeRepository;
import cre.gutwo.dao.jpa.DeviceInfoRepository;
import cre.gutwo.domain.CompanyCode;
import cre.gutwo.domain.DeviceInfo;
import cre.gutwo.imp.RuleImpUtils;
import cre.gutwo.util.Constants;

/**
 * @author Cre.Gu
 *
 */
@Service
public class RuleServiceImpl implements RuleService {

	@Resource
	private CompanyCodeRepository companyCodeRepository;
	@Resource
	private DeviceInfoRepository deviceInfoRepository;

	@Override
	public void impCompanyCode(List<CompanyCode> list) {
		if (list != null) {
			companyCodeRepository.save(list);
		}
	}

	@Override
	public void impDeviceInfo(List<DeviceInfo> list) {
		if (list != null) {
			deviceInfoRepository.save(list);
		}
	}

	@Override
	public void deleteAllCompanyCode() {
		companyCodeRepository.deleteAll();
	}

	@Override
	public void deleteAllDeviceInfo() {
		deviceInfoRepository.deleteAll();
	}

	@Override
	public String changeValDi(String deviceName, String spec) {
		List<DeviceInfo> dis = deviceInfoRepository.findByDeviceName(deviceName);
		if (dis.size() == 0)
			return "";

		DeviceInfo di = dis.get(0);
		if (di == null)
			return "";

		if (Constants.LINKAGE_ID.equals(spec)) {
			return di.getLinkageID();
		} else if (Constants.MACADDR.equals(spec)) {
			return di.getMacAddr();
		} else if (Constants.IPADDR.equals(spec)) {
			return di.getIpAddr();
		} else if (Constants.OS.equals(spec)) {
			return di.getOs();
		} else if (Constants.OS_LEVEL.equals(spec)) {
			return di.getOsLevel();
		} else if (Constants.DEVICE_ID.equals(spec) || Constants.PCID.equals(spec)) {
			return di.getLinkageID() + "-" + di.getUniqueKey();
		}
		return "";
	}

	@Override
	public String changeValCc(String lid, String spec) {
		List<CompanyCode> ccs = companyCodeRepository.findByName(lid);

		if (ccs.size() == 0)
			return "";

		CompanyCode cc = ccs.get(0);
		if (cc == null)
			return "";

		if (Constants.COMPANYID.equals(spec)) {
			return cc.getCode();
		}
		return "";
	}

	@Override
	public void initAll() {
		deleteAllCompanyCode();
		deleteAllDeviceInfo();

		String ccsf = "E:\\gutwo\\companycode.xlsx";
		String disf = "E:\\gutwo\\deviceinfo.xlsx";

		List<CompanyCode> ccs = RuleImpUtils.gainCompanyCodeList(ccsf);
		List<DeviceInfo> dis = RuleImpUtils.gainDeviceInfoList(disf);

		impCompanyCode(ccs);
		impDeviceInfo(dis);
	}

	@Override
	public DeviceInfo findByDeviceName(String deviceName) {
		List<DeviceInfo> dis = deviceInfoRepository.findByDeviceName(deviceName);
		if (dis.size() == 0)
			return null;

		return dis.get(0);
	}

	@Override
	public CompanyCode findCcByName(String name) {
		List<CompanyCode> ccs = companyCodeRepository.findByName(name);

		if (ccs.size() == 0)
			return null;

		return ccs.get(0);
	}

	@Override
	public List<DeviceInfo> findDis() {
		return deviceInfoRepository.findAll();
	}

	@Override
	public List<CompanyCode> findCcs() {
		return companyCodeRepository.findAll();
	}

	@Override
	public void initDeviceInfo(String file) {
		deleteAllDeviceInfo();

		List<DeviceInfo> dis = RuleImpUtils.gainDeviceInfoList(file);
		impDeviceInfo(dis);
	}

	@Override
	public void initCompanyCode(String file) {
		deleteAllCompanyCode();

		List<CompanyCode> ccs = RuleImpUtils.gainCompanyCodeList(file);
		impCompanyCode(ccs);
	}

	@Override
	public boolean deviceInfoEmpty() {
		return deviceInfoRepository.count() == 0l;
	}

	@Override
	public boolean companyCodeEmpty() {
		return companyCodeRepository.count() == 0l;
	}

}
